package com.example.demo;

import com.sun.jdi.StackFrame;
import jakarta.servlet.http.HttpSession;
import org.apache.catalina.util.StringUtil;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class LoginController {
    @RequestMapping("/login")
    public boolean login(String userName, String userPassword, HttpSession session) {
        //判空,StringUtils.hasLength判断给定的字符串 str 是否不为 null 且长度大于 0 。
        if (!StringUtils.hasLength(userName) || !StringUtils.hasLength(userPassword)) {
            return false;
        }
        //校验账号密码是否正确
        if (!"zhangsan".equals(userName)||!"123456".equals(userPassword)){
            return false;
        }
        session.setAttribute("userName",userName);
        return true;
    }
    @RequestMapping("/getLonginUser")
    public String getLonginUser(HttpSession session){
        String userName=(String) session.getAttribute("userName");
        if (StringUtils.hasLength(userName)){
            return userName;
        }
        return "";
    }
}
